﻿using System;
using System.Text;
using System.Windows.Media;
using System.Xml;
using Fiddler;
using System.IO;
using System.Diagnostics;

namespace BinaryXmlInspector
{
    public class BinaryViewModel
    {
        public BinaryViewModel(Action<string> setText)
        {
            SetText = setText;
            FontSize = CONFIG.flFontSize;
        }
        private Action<string> SetText;
        private WcfBinaryCodec m_wcfBinaryCodec = new WcfBinaryCodec(Encoding.UTF8);

#region Properties from Fiddler configuration

        private float m_fontSize;
        public float FontSize
        {
            get { return m_fontSize; }
            set { m_fontSize = value * 1.4f; }
        }
        public string FontFamily
        {
            get { return "Segoe UI"; }
        }
        public Color EnableColor 
        {
            get { return Colors.White; }
        }
        public Color DisableColor
        {
            get
            {
                return Color.FromArgb(CONFIG.colorDisabledEdit.A,
                                      CONFIG.colorDisabledEdit.R,
                                      CONFIG.colorDisabledEdit.G,
                                      CONFIG.colorDisabledEdit.B);
            }
        }

#endregion

#region Dynamic Properties

        public bool ReadOnly
        {
            get; set;
        }

        private byte[] m_encodedXML;
        public byte[] EncodedXML
        {
            get { return m_encodedXML; }
            set
            {
                m_encodedXML = value;
                SetText(DecodeBinaryXML(m_encodedXML));
                Dirty = false;
            }
        }

        public bool Dirty
        {
            get; set;
        }

        public void Clear()
        {
            EncodedXML = null;
        }

        public string LinePosition
        {
            get { return ""; }  // TODO "0: 0"
        }

        public string BytePosition
        {
            get { return ""; }  // TODO "0/20000"
        }

        public bool Pretty
        {
            get; set;
        }

        public bool Explode
        {
            get; set;
        }

#endregion

#region Binary XML helper methods

        public string DecodeBinaryXML(byte[] encodedXML)
        {
            if (encodedXML == null)
            {
                return "";
            }

            try
            {
                return m_wcfBinaryCodec.DecodeBinaryXML(encodedXML, Explode);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                return "";
            }
        }

        public byte[] EncodeBinaryXML(string xml)
        {
            if (String.IsNullOrEmpty(xml.Trim()))
            {
                return null;
            }

            try
            {
                return m_wcfBinaryCodec.EncodeBinaryXML(xml);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                return null;
            }
        }
        
#endregion

    }
}
